package a07_二叉树;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * <p>
 * a03_层序遍历01_二叉树的层序遍历
 * </p>
 *
 * @author flyduck
 * @since 2024-08-20
 */
public class a05_层序遍历05_N叉树的层序遍历 {

    public static void main(String[] args) {
//        TreeNode root = new TreeNode(
//                6,
//                new TreeNode(4,new TreeNode(1),new TreeNode(3)),
//                new TreeNode(7,new TreeNode(5),new TreeNode(8)));
        TreeNode root = new TreeNode(
                6,
                new TreeNode(4),
                new TreeNode(7,new TreeNode(5),new TreeNode(8)));

    }

    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> resultList = new ArrayList<>();
        if (root == null) {
            return resultList;
        }

        Queue<Node> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            int popSize = queue.size();
            List<Integer> result = new ArrayList<>();
            while (popSize > 0) {
                Node poll = queue.poll();
                result.add(poll.val);
                for (Node child : poll.children) {
                    queue.add(child);
                }
                popSize--;
            }
            resultList.add(result);
        }
        return resultList;
    }


}
